package com.xmb.wechat.view.wechat.money;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.BankCardBean;
import com.xmb.wechat.definterface.SimpleTextWatcher;
import com.xmb.wechat.definterface.onBankCardChooseCallBack;
import com.xmb.wechat.dialog.BankDialog;
import com.xmb.wechat.widget.TitleLayout;

import butterknife.BindView;
import butterknife.OnClick;
import io.objectbox.Box;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/17 10:14
 */
public class WechatFundBankCardAddActivity extends BaseActivity {

    @BindView(R2.id.btn_confirm)
    Button      mBtnConfirm;
    @BindView(R2.id.et_remark)
    EditText    mEtRemark;
    @BindView(R2.id.et_number)
    EditText    mEtNumber;
    @BindView(R2.id.tv_bank_card)
    TextView    mTvBankCard;
    @BindView(R2.id.title_layout)
    TitleLayout mTitleLayout;

    private Box<BankCardBean> mBankCardBeanBox;
    private BankCardBean      mBankCardBean;
    private BankDialog        mBankDialog;

    public WechatFundBankCardAddActivity() {
        super(R.layout.activity_fund_bank_card_add);
    }

    public static void startForResult(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, WechatFundBankCardAddActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void initView() {
        mTitleLayout.setRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doConfirm();
            }
        });
        mBankCardBeanBox = WechatStartApplication.getBoxStore(this).boxFor(BankCardBean.class);
        mBankCardBean = new BankCardBean();
        mEtNumber.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChangedFinish(String value) {
                mBankCardBean.setBankNumber(value.trim());
            }
        });
        mEtRemark.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChangedFinish(String value) {
                mBankCardBean.setRemark(value.trim());
            }
        });
    }

    @OnClick({R2.id.btn_confirm, R2.id.re_bank_card})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.btn_confirm) {
          doConfirm();
        } else if (id == R.id.re_bank_card) {
            if (mBankDialog == null) {
                mBankDialog = new BankDialog(this, new onBankCardChooseCallBack() {

                    @Override
                    public void onClick(BankCardBean bankCardBean) {
                        mBankCardBean.setAvatar(bankCardBean.getAvatar());
                        mBankCardBean.setBankName(bankCardBean.getBankName());
                        mTvBankCard.setText(bankCardBean.getBankName());
                    }

                    @Override
                    public void onLongClick(BankCardBean bankCardBean) {
                        //nothing
                    }
                });
            }
            mBankDialog.show();
        }
    }

    private void doConfirm() {
        String cardNumber = mEtNumber.getText().toString().trim();
        if (TextUtils.isEmpty(cardNumber)) {
            ToastUtils.showShort("卡号不能为空");
            return;
        }
        if (cardNumber.length() < 4) {
            ToastUtils.showShort("卡号后位数不能少于4位");
            return;
        }
        String bankName = mTvBankCard.getText().toString().trim();
        if (TextUtils.equals(bankName, "请选择")) {
            ToastUtils.showShort("请选择储蓄卡");
            return;
        }
        mBankCardBeanBox.put(mBankCardBean);
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBankDialog != null) {
            mBankDialog.dismiss();
            mBankDialog = null;
        }
    }

}
